<?php
namespace Home\Controller;
use Think\Controller;
defined('IN_ECTOUCH') or die('Deny Access');
class UrlController extends Controller {

    //旅行社佣金查看链接
     public function mshop_url_login()
    {
        /*测试*/
        // $mshop_id=2;


        $mshop_id=I('mshop_id',0);
        $pwd=I('pwd',0);




        if(!$mshop_id){$this->error('链接有误',U('index/index'));}

        $data=array();
        $data['mshop_id']=$mshop_id;

        if($_POST['pwd'])//存在密钥---验证密钥查看信息
        {
           
           $url_id=M("mshop_build_url")->where("mshop_id='$mshop_id' and org_pwd='$pwd'")->getField("url_id");

           // print_r(M("mshop_build_url")->_sql());exit();

           if(!$url_id){$this->error('密钥有误');}

           $_SESSION['url_mshop_id']=$mshop_id;
           $this->redirect("url/mshop_profit_url");


        }
        else //登录-==
        {
           $data['mshop_name']=M("mshop")->where("mshop_id='$mshop_id'")->getField("mshop_name");
           if(!$data){$this->error('该旅行社不存在',U('index/index'));}
           $this->assign("is_show",0);

           
        }
      $this->assign("data",$data);
      $this->display();
    }

        //旅行社佣金查看链接
     public function mshop_profit_url()
    {
        if(empty($_SESSION['url_mshop_id']))
        {
           $this->redirect("url/mshop_url_login",array('mshop_id',I("mshop_id")));
        }
        $mshop_id=$_SESSION['url_mshop_id'];


         $mshop_info=D('mshop')->get_info($mshop_id);

         // print_r($mshop_info);exit();
         $data=$mshop_info;
         $this->assign("is_show",1);
         $this->assign("data",$data);
         $this->display('mshop_profit_url');
    }
    //下载excel
    public function down_excel()
    {
         $RATH = dirname(dirname(dirname(dirname(dirname(__FILE__)))));


        if(empty($_SESSION['url_mshop_id']))
        {
           $this->redirect("url/mshop_url_login",array('mshop_id',I("mshop_id")));
           exit();
        }


        $mshop_id=$_SESSION['url_mshop_id']; 
        
          $html         = '';  


          Vendor('PHPExcel.PHPExcel');
  
          // echo date('H:i:s') . " Create new PHPExcel object\n";  
          $objPHPExcel = new \PHPExcel();  
        
          // echo date('H:i:s') . " Set properties\n";  
          $objPHPExcel->getProperties()->setCreator("wdz")->setLastModifiedBy("wdz")->setTitle("我的订单")->setSubject("我的订单")->setDescription(date('Y/m/d H:i:s') . "导出的订单")->setKeywords("我的订单")->setCategory("Test result file");  
        
          /*设置标题属性*/  
          //字体大小  
          $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setSize(16);  
          //加粗  
          $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true);  
          //水平居中  
          $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::VERTICAL_CENTER);  
          //垂直居中  
          $objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);  
          //表格宽度  
          $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);  
          $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);  
          $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);  
          $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);  
          $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50);  
          $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
          $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(100);   
          $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20); 
        
          // echo date('H:i:s') . " Add some data\n";  
          $objPHPExcel->setActiveSheetIndex(0);  
          $objPHPExcel->getActiveSheet()->setCellValue('A1', '订单号'); 
          $objPHPExcel->getActiveSheet()->setCellValue('B1', '旅行社名称');  
          $objPHPExcel->getActiveSheet()->setCellValue('C1', '佣金');  
          $objPHPExcel->getActiveSheet()->setCellValue('D1', '商品总价');  
          $objPHPExcel->getActiveSheet()->setCellValue('E1', '确认收货时间');  
          $objPHPExcel->getActiveSheet()->setCellValue('F1', '状态');  
          $objPHPExcel->getActiveSheet()->setCellValue('G1', '商品详情(名称-价格-数量)'); 
          $objPHPExcel->getActiveSheet()->setCellValue('H1', '推荐导游');  

            
          $hang = 2;  
          //////////////////////////////////////////////////////////////



          $promote_start_date=$_POST['promote_start_date']?strtotime($_POST['promote_start_date']):'';
          $promote_end_date=$_POST['promote_end_date']?strtotime($_POST['promote_end_date']):'';
          $Where_time=" and m.mshop_id='$mshop_id'";
          if($promote_start_date)
          {
              $Where_time.=" and m.ctime>=".$promote_start_date;
          }
          if($promote_end_date)
          {
              $Where_time.=" and m.ctime<=".$promote_end_date;
          }

              $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'm.frozen_id' : trim($_REQUEST['sort_by']);
              $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);


              /* 查询 */
              $sql = "SELECT m.*,u.mshop_name ".
                      "FROM " .C("DB_PREFIX").'mshop_frozen_profit as m'.
                      " left join ". C("DB_PREFIX")."mshop as u on u.mshop_id=m.mshop_id ".
                      " WHERE m.frozen_profit>0 and m.status<2   $Where_time ".
                      " ORDER BY $filter[sort_by] $filter[sort_order] ";
          $res = M()->query($sql);

          // print_r( $res);exit();


          if(!$res)
          {$this->error('当前条件无已佣金记录，导出失败。');exit;}
        
        foreach ($res as $key => &$value) {

          $value['ctime']=date('Y-m-d H:i:s',$value['ctime']);  
          if($value['status']==2)  $value['status']="取消";else $value['status']="正常";

          //商品明细
          $sql="select o.user_id,og.goods_name,og.goods_price,og.goods_number ".
                     "FROM " . C("DB_PREFIX").'order_info as o'.
                      " left join ". C("DB_PREFIX")."order_goods as og on og.order_id=o.order_id ".
                      " WHERE o.order_sn=".$value['order_sn'];
          $goods =M()->query($sql);
          $goods_name='';
          foreach ($goods as $goods_key => $goods_value) {
            $goods_name.=$goods_value['goods_name'].'-'.$goods_value['goods_price'].'-'.$goods_value['goods_number'].',';
          }

          $order_user_id= $goods[0]['user_id'];
          if($order_user_id)
          {
                //推荐导游
            $sql="select u.user_name,u.mobile_phone ".
                       "FROM " . C("DB_PREFIX").'users_mshop as up'.
                        " left join ". C("DB_PREFIX")."users as u on u.user_id=up.user_pid ".
                        " WHERE up.user_id='$order_user_id'";
            $users_mshop_data =M()->query($sql); 
            $user_info= $users_mshop_data[0];
          }
          $tjr_name="未知,请联系后台管理员";
          if($user_info)
          {
            $tjr_name=$user_info['user_name']."(".$user_info['mobile_phone'].")";
          }



          $objPHPExcel->getActiveSheet()->setCellValue('A'.$hang, $value['order_sn']);  
          $objPHPExcel->getActiveSheet()->getStyle('A'.$hang)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER);  

          $objPHPExcel->getActiveSheet()->setCellValue('B'.$hang, $value['mshop_name']);  
          $objPHPExcel->getActiveSheet()->setCellValue('C'.$hang, $value['frozen_profit']);  
          $objPHPExcel->getActiveSheet()->setCellValue('D'.$hang, $value['goods_price']);  

          $objPHPExcel->getActiveSheet()->setCellValue('E'.$hang, $value['ctime']);
          $objPHPExcel->getActiveSheet()->setCellValue('F'.$hang, $value['status']);
          $objPHPExcel->getActiveSheet()->setCellValue('G'.$hang, $goods_name);
          $objPHPExcel->getActiveSheet()->setCellValue('H'.$hang, $tjr_name);

          $hang++;

        }


          $objPHPExcel->getActiveSheet()->setTitle(("我的佣金报表"));  
          $objPHPExcel->setActiveSheetIndex(0);  


         Vendor('PHPExcel.PHPExcel.IOFactory');

         $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
         $file_name="profit_list.xls";
         $objWriter->save($RATH.'/file_temp/'.$file_name);
        
          $url="../../../file_temp/". $file_name;


          header("Location: $url\n"); 
          exit;  

    }

   

}